﻿using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NPOI.SS.Formula.Functions;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.ProductionSystem;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    public class WorkmanshipController : Controller
    {
        private readonly WorkmanshipService _workmanshipService;
        public WorkmanshipController(WorkmanshipService workmanshipService)
        {
            _workmanshipService = workmanshipService;
        }
        public IActionResult Workmanships()
        {
            return View();
        }
        [HttpPost]
        public async Task<int> AddWorkmanship(WorkmanshipDTO workmanship)
        {
            return await _workmanshipService.AddWorkmanship(workmanship);
        }
        [HttpDelete]
        public async Task<int> DeleteWorkmanship(int workmanshipId)
        {
            return await _workmanshipService.DeleteWorkmanship(workmanshipId);
        }
        [HttpGet]
        public async Task<WorkmanshipDTO> FanWorkmanship(int workmanshipId)
        {
            return await _workmanshipService.FanWorkmanship(workmanshipId);
        }
        [HttpGet]
        public async Task<JsonResult> GetWorkmanship(int pageIndex = 1, int pageSize = 10)
        {
            var list = await _workmanshipService.GetWorkmanship();
            var result = list.OrderBy(c=>c.Id).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code = 0,
                msg = "",
                count = list.Count,
                data = result,
            });
        }
        [HttpPost]
        public async Task<int> UpdateWorkmanship(WorkmanshipDTO workmanship)
        {
            return await _workmanshipService.UpdateWorkmanship(workmanship);
        }
    }
}
